<template>
	<view class="page">
		<myNavBar barIcon="back" title="协办人员" right-text="解散协办" @clickRight="clickRight"></myNavBar>
		<view v-if="$store.state.userStore.userInfo.type == 1" class="group-invite">
			<view class="search-bar">
				<uni-search-bar v-model="searchText" cancelButton="none" placeholder="输入好友昵称搜索"></uni-search-bar>
			</view>
			<view class="friend-items">
				<scroll-view class="scroll-bar" scroll-with-animation="true" scroll-y="true">
					<view v-for="friend in friendItems" v-show="!searchText || friend.nickName.startsWith(searchText)"
						:key="friend.id">
						<view class="friend-item" @click="onSwitchChecked(friend)">
							<head-image :name="friend.nickName" :online="friend.online" :url="friend.headImage"
								:size="100"></head-image>

							<view class="friend-name">{{ friend.nickName}}</view>
							<view class="friend-checked">
								<radio :checked="friend.checked" :disabled="friend.disabled" @click.stop="onSwitchChecked(friend)" />
							</view>
						</view>

					</view>
				</scroll-view>
			</view>
			<view class="inviteBtn">
				<button type="primary" :disabled="inviteSize==0" @click="onInviteFriends()">保存协办({{inviteSize}}) </button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				groupId: null,
				searchText: "",
				groupMembers: [],
				friendItems: []
			}
		},
		methods: {
			onInviteFriends() {
				let inviteVo = {
					groupId: this.groupId,
					friendIds: []
				}
				this.friendItems.forEach((f) => {
					if (f.checked && !f.disabled) {
						inviteVo.friendIds.push(f.id);
					}
				})
				if (inviteVo.friendIds.length > 0) {
					this.$http({
						url: "/group/invite",
						method: 'POST',
						data: inviteVo
					}).then(() => {
						uni.showToast({
							title: "邀请成功",
							icon: 'none'
						})
						setTimeout(() => {
							// 回退并刷新
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							prevPage.$vm.loadGroupMembers();
							uni.navigateBack();
						}, 1000);

					})
				}
			},
			onShowUserInfo(userId) {
				uni.navigateTo({
					url: "/pages/common/user-info?id=" + userId
				})
			},
			onSwitchChecked(friend) {
				if (!friend.disabled) {
					friend.checked = !friend.checked;
				}
				console.log(this.inviteSize)
			},
			initFriendItems() {
				this.friendItems = [];
				let friends = this.$store.state.friendStore.friends;
				friends.forEach((f => {
					if (f.delete) {
						return
					}
					let item = {
						id: f.id,
						headImage: f.headImage,
						nickName: f.nickName,
						online: f.online
					}
					item.disabled = this.isGroupMember(f.id);
					item.checked = item.disabled;
					this.friendItems.push(item);
				}))
			},
			loadGroupMembers(id) {
				this.$http({
					url: `/group/members/${id}`,
					method: "GET"
				}).then((members) => {
					this.groupMembers = members.filter(m => !m.quit);
					this.initFriendItems();
				})
			},

			isGroupMember(id) {
				return this.groupMembers.some(m => m.userId == id);
			},
			clickRight() {
				uni.showModal({
					title: '确认要解散协办吗?',
					success: (res) => {
						if (res.confirm) {}
					}
				});
			}
		},
		computed: {
			inviteSize() {
				return this.friendItems.filter(f => !f.disabled && f.checked).length;
			}
		},
		onLoad(options) {
			this.groupId = options.id;
			this.loadGroupMembers(options.id);
		}
	}
</script>

<style lang="scss" scoped>
	.group-invite {
		height: calc(100vh - 100px);
		position: relative;
		border: #dddddd solid 1px;
		display: flex;
		flex-direction: column;

		.friend-items {
			position: relative;
			flex: 1;
			overflow: hidden;

			.friend-item {
				height: 120rpx;
				display: flex;
				margin-bottom: 1rpx;
				position: relative;
				padding: 0 30rpx;
				align-items: center;
				background-color: white;
				white-space: nowrap;

				.friend-name {
					flex: 1;
					padding-left: 20rpx;
					font-size: 30rpx;
					font-weight: 600;
					line-height: 60rpx;
					white-space: nowrap;
					overflow: hidden;
				}
			}

			.scroll-bar {
				height: 100%;
			}
		}
	}

	.inviteBtn {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>